package com.hmkj.web.controller.pay;

import com.etonepay.b2c.entity.BackForm;
import com.hmkj.core.api.etonepay.EtonePayUtil;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.pay.PayResult;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.service.amount.CashService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(description = "易通金服异步回调")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/notify/etone")
public class EtonePayNotifyController extends GenericController<Integer, Recharge> {

    @Autowired
    RechargeService rechargeService;

    @Autowired
    CashService cashService;

    @ApiOperation(value = "支付回调", notes = "支付回调")
    @ResponseBody
    @RequestMapping(value = "/payNotify", method = RequestMethod.POST)
    public String payNotify(HttpServletRequest request) {

        BackForm backForm = EtonePayUtil.buildForm(
                request.getParameter("transCode"),      // 交易代码
                request.getParameter("merchantId"),     // 商户编号
                request.getParameter("respCode"),       // 返回码
                request.getParameter("sysTraceNum"),    // 商户请求流水号
                request.getParameter("merOrderNum"),    // 商户订单号
                request.getParameter("orderId"),        // 支付网关订单号
                request.getParameter("bussId"),         // 业务代码
                request.getParameter("tranAmt"),        // 实际交易金额
                request.getParameter("orderAmt"),       // 订单金额
                request.getParameter("bankFeeAmt"),     // 支付渠道手续费
                request.getParameter("integralAmt"),    // 积分抵扣金额
                request.getParameter("vaAmt"),          // 虚拟账户支付金额
                request.getParameter("bankAmt"),        // 支付渠道支付金额
                request.getParameter("bankId"),         // 支付渠道 ID
                request.getParameter("integralSeq"),    // 积分交易流水号
                request.getParameter("vaSeq"),          // 虚拟账户交易流水号
                request.getParameter("bankSeq"),        // 支付机构交易流水号
                request.getParameter("tranDateTime"),   // 交易时间
                request.getParameter("payMentTime"),    // 支付时间
                request.getParameter("settleDate"),     // 清算日
                request.getParameter("currencyType"),   // 货币代码
                request.getParameter("orderInfo"),      // 订单信息
                request.getParameter("userId"),         // 用户 ID
                request.getParameter("signValue"));     // 数字签名
        String hmTk = request.getParameter("reserver1");// 支付标识
        if (!EtonePayUtil.chackSignValue(backForm)){
            // 缓存回调结果
            redisService.put(CacheID.PAY_RESULT + hmTk, PayResult.failed("抱歉，充值遇到问题，请联系客服处理", ""), 30 * 60);
            throw new BussinessException("验签失败");
        }
        rechargeService.doRechargeBackEtone(backForm, hmTk);
        return "success";
    }

}
